<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
    /*String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";*/
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <%@ include file="/common/common-css.v2.jsp" %>
    <style>
        #jquery-table td {
            cursor: pointer !important;
            word-break: break-all;
            word-wrap: break-word;
        }

        .load_bg {
            background: #000;
            opacity: 0.1;
            left: 0;
            top: 0;
            position: fixed;
            height: 100%;
            width: 100%;
            overflow: hidden;
            z-index: 10000;
        }

        .load_img {
            left: 50%;
            top: 180px;
            position: fixed;
            height: 50px;
            overflow: hidden;
            z-index: 20000;
        }

        .dialog-box {
            position: fixed;
            right: 200px;
            top: 200px;
            background-color: #fff;
            padding: 15px;
            box-shadow: 1px 1px 3px rgba(0,0,0,.5);
            z-index: 9999;
            border: 1px solid #ddd;
            display: none;
        }

        .dialog-box td {
            height: 20px;
            padding: 5px;
        }

        .btn-box {
            text-align: center;
        }

        .td-title {
            text-align: right;
        }
    </style>
</head>
<body class="no-skin">
<div class="load_bg" style="display:none;"></div>
<img src="http://img.xmiles.cn/commentloading.gif" class="load_img" style="display:none"/>

<div class="page-content">
    <form id="queryForm">
        <div class="page-content-area">
            <div class="row">
                <div class="col-sm-12">
                    <div class="widget-box" style="border:0 solid #ccc; border-top:none;border-bottom:none;">
                        <div class="widget-header widget-header-blue widget-header-flat">
                            <h4 class="widget-title">选择条件进行查询</h4>

                            <div class="widget-toolbar">
                                <a href="#" data-action="collapse">
                                    <i class="ace-icon fa fa-chevron-up"></i>
                                </a>
                            </div>
                        </div>

                        <div class="widget-body">
                            <div class="widget-main clearfix">

                                <div class="form-group col-xs-12 col-sm-4">
                                    <label class="col-sm-4 control-label" style="text-align:right"
                                           for="startDate">开始时间:</label>
                                    <div class="col-sm-8">
                                        <div class="input-daterange input-group">
                                            <input type="text" class="date-picker form-control"
                                                   data-date-format="yyyy-mm-dd" style="padding-left:0"
                                                   name="startDate" id="startDate" placeholder="请选择红包发放开始时间">
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group col-xs-12 col-sm-4">
                                    <label class="col-sm-4 control-label" style="text-align:right"
                                           for="endDate">结束时间:</label>
                                    <div class="col-sm-8">
                                        <div class="input-daterange input-group">
                                            <input type="text" class="date-picker form-control"
                                                   data-date-format="yyyy-mm-dd" style="padding-left:0"
                                                   name="endDate" id="endDate" placeholder="请选择红包发放结束时间">
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group col-xs-12 col-sm-4">
                                    <label class="col-sm-4 control-label" for="activityName"
                                           style="text-align:right;">活动名称:</label>
                                    <div class="col-sm-8">
                                        <s:select id="activityName" name="activityName" list="%{activityNameList}" cssClass="form-control" headerKey="" headerValue="全选"/>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-md-12 center">
                    <div class="clearfix form-actions">
                        <button class="btn btn-purple no-border" type="button" name="btn-query" onclick="query()"
                                id="btn-query">
                            <i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
                            查询
                        </button>
                        &nbsp;&nbsp;&nbsp;
                        <button class="btn btn-purple no-border" type="button" onclick="exportCsv()">
                            <i class="ace-icon fa fa-undo icon-on-right bigger-110"></i>
                            导出
                        </button>
                        &nbsp;&nbsp;&nbsp;
                        <button class="btn btn-grey no-border" type="reset">
                            <i class="ace-icon fa fa-undo icon-on-right bigger-110"></i>
                            重置
                        </button>
                        &nbsp;&nbsp;&nbsp;
                        <button class="btn btn-purple no-border" type="button" onclick="manualSync()">
                            <i class="ace-icon fa fa-undo icon-on-right bigger-110"></i>
                            手动同步红包状态
                        </button>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-xs-12">
                    <table id="jquery-table" class="table table-striped table-bordered table-hover">
                    </table>
                </div>
            </div>

        </div>
    </form>
</div>


<%@ include file="/common/common-js.jsp" %>
<script type="text/javascript">

    var loadDom = $('.load_bg,.load_img');
    var jqTable = null;

    function query() {
        loadDom.show();
        if (jqTable != null) {
            jqTable.fnDraw();
        }
        else {
            loadData();
        }
    }

    function exportCsv(){
        var form = document.forms["queryForm"];
        var url0 = ctx + "/weixin/weixin_redpack_export_stat_list.action";
        var url = encodeURI(url0);
        form.action= url;
        form.submit();
    }
    
    function manualSync(){
    	var GLOBAL_URL = 'http://' + location.host + '/';
    	var requestUrl = GLOBAL_URL + "weixin_service/common?funid=18&" + new Date().getTime();
    	var requestData = {
            "data": {
                "phead": {}
            },
            "shandle": "0",
            "handle": "0"
        };
        requestData = JSON.stringify(requestData);

        $.ajax({
            type: "post",
            timeout: 30000,
            url: requestUrl,
            data: requestData,
            contentType: "application/json",
            dataType: "json",
            beforeSend: function () {
            	loadDom.show();
            },
            success: function (data) {
            	loadDom.hide();
                if (data.result.status == 1) {
                	if (data.count > 0) {
                		var seconds = data.count / 4;
                		if (seconds >= 60) {
		                	var minus = seconds / 60;
		                	alert("更新" + data.count + "记录，大概需要" + minus + "分钟");
                		} else {
                			alert("更新" + data.count + "记录，大概需要" + seconds + "秒");
                		}
                	} else {
                		alert("数据已经同步，无需重复操作");
                	}
                } else {
                	alert(data.result.msg);
                }
            },
            error: function (xhr, status, errorThrown) {
            	loadDom.hide();
                console.log(xhr, status, errorThrown);
            },
            complete: function () {
            	loadDom.hide();
            }
        });
    }

    var loadData = function () {
        jqTable = $('#jquery-table').dataTable({
            "columnDefs": [
                {
                    "title": '日期',
                    "targets": [0],
                    "data": "cdate",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '活动名称',
                    "targets": [1],
                    "data": "act_name",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '单个红包金额',
                    "targets": [2],
                    "data": "fee",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '发出红包数',
                    "targets": [3],
                    "data": "send_num",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '领取红包数',
                    "targets": [4],
                    "data": "recv_num",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '发出总金额',
                    "targets": [5],
                    "data": "send_amount",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '领取总金额',
                    "targets": [6],
                    "data": "recv_amount",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '未领取金额',
                    "targets": [7],
                    "data": "no_recv_amount",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '商户平台退款金额',
                    "targets": [8],
                    "data": "refund_amount",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '商户平台领取金额',
                    "targets": [9],
                    "data": "mch_send_amount",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                },
                {
                    "title": '差异',
                    "targets": [10],
                    "data": "diff_amount",
                    "class": "center",
                    "bSortable": false,
                    "width": "5%"
                }
            ],
            "sAjaxSource": ctx + "/weixin/weixin_redpack_get_stat_list.action",
            "fnServerData": function (url, aoData, fnCallback) {
                var data = $('#queryForm').serializeObject();
                data.aoData = JSON.stringify(aoData);
                $.ajax({
                    "type": "POST",
                    "dataType": 'json',
                    "url": url,
                    "data": data,
                    "success": function (resp) {
                        try {
                            fnCallback(resp);
                        } catch (e) {
                            alert(e.name + ' :' + e.message)
                        }
                        //initListPage();
                    },
                    "error": function (XMLHttpRequest, textStatus) {
                        alert(textStatus);
                        loadDom.hide();
                    },
                    "complete": function () {
                        loadDom.hide();
                    }
                });
            },
            "sScrollXInner": "100%",
            "sScrollY": "100%",
            "aaSorting": [[0, "asc"]]
        });
    };

    jQuery(function ($) {
        $(".date-picker").datepicker({autoclose: true}).next().on(ace.click_event, function () {
            $(this).prev().focus();
        });
        $("#activityName").combobox();
    });
</script>
</body>
</html>